package com.scedu.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Aspect
@Component
public class HttpAspect {
    private  final static Logger logger= LoggerFactory.getLogger(HttpAspect.class);//import org.slf4j.Logger;
//    @Before("execution(public * com.scedu.controller.GirlController.*(..))")
//    public void  log(){
//        System.out.println("log log log");
//    }
//    @After("execution(public * com.scedu.controller.GirlController.*(..))")
//    public  void logafter(){
//        System.out.println("end end end");
//    }
      @Pointcut("execution(public * com.scedu.controller.GirlController.*(..))")
      public void log(){

      }
      @Before("log()")
      public  void dobefore(JoinPoint joinPoint){
          ServletRequestAttributes attributes=(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
          HttpServletRequest request=attributes.getRequest();
          //记录 URL
          logger.info("url={}",request.getRequestURL());

          //记录 请求方法
          logger.info("method={}",request.getMethod());

          //记录 客户端Ip
          logger.info("client ip={}",request.getRemoteAddr());

          //记录 类名
          logger.info("class name={}",joinPoint.getSignature().getDeclaringTypeName());

          //记录 类方法
          logger.info("class method={}",joinPoint.getSignature().getName());

          //记录 参数
          logger.info("args={}",joinPoint.getArgs());

          logger.info("begin begin begin");
      }
      @After("log()")
       public void doafter(){
          //System.out.println("end end end");
          logger.info("end end end");
      }
      @AfterReturning(returning = "object",pointcut = "log()")
      public void doafterreturning(Object object){
       logger.info("response={}",object.toString());
      }
}
